package com.aitemv.services.impl;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.stereotype.Service;

import com.aitemv.base.BaseDao;
import com.aitemv.base.impl.BaseServiceImpl;
import com.aitemv.entity.UserEntity;
import com.aitemv.services.UserService;
import com.aitemv.utils.PageBean;
import com.aitemv.utils.PropertiesUtil;

@Service
public class UserServiceImpl extends BaseServiceImpl implements UserService {

	@Autowired
	private BaseDao baseDao;

	public void addUser(UserEntity user) {
		String sql = "";
		try {
			sql = PropertiesUtil.load("user_register");
		} catch (IOException e) {
			System.err.println("--注册用户除了异常");
			e.printStackTrace();
		}
		baseDao.saveOrUpdateObject(sql, user);
	}

	public boolean checkAcc(String acc) {
		String sql = "";
		try {
			sql = PropertiesUtil.load("user_checkAcc");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int countAcc = baseDao.isExist(sql, new String[] { acc });
		if (countAcc > 0) {
			return false;
		} else {
			return true;
		}
	}

//	public List<BaseEntity> findUserList(PageBean page) {
//		StringBuffer sql = new StringBuffer();
//		String pageSql = " limit "+page.getStartPage()+","+page.getPageSize();
//		try {
//			sql.append(PropertiesUtil.load("user_findAll"));
//			sql.append(pageSql);
//		} catch (IOException e) {
//			System.err.println("--查询用户列表--exc");
//			e.printStackTrace();
//		}
//		ParameterizedRowMapper<UserEntity> mapper = new ParameterizedRowMapper<UserEntity>() { 
//		    
//	        // notice the return type with respect to Java 5 covariant return types 
//	        public UserEntity mapRow(ResultSet rs, int rowNum) throws SQLException { 
//	        	UserEntity actor = new UserEntity(); 
//	        	actor.setRemark(rs.getString("remark"));
//	        	actor.setTxt1(rs.getString("txt1"));
//	        	actor.setTxt2(rs.getString("txt2"));
//	        	actor.setUserAccount(rs.getString("userAccount"));
//	        	actor.setUserCtime(rs.getString("userCtime"));
//	        	actor.setUserId(rs.getInt("userId"));
//	        	actor.setUserName(rs.getString("userName"));
//	        	actor.setUserPassword(rs.getString("userPassword"));
//	        	actor.setUserRole(rs.getInt("userRole"));
//	            return actor; 
//	        } 
//	    }; 
//
//		List<BaseEntity>  userList = baseDao.getObjList(sql.toString(), mapper, null);
//		return userList;
//	}
	
	
	
	public List<UserEntity> findUserList(PageBean page,List<Object> param,String where) throws IOException {
//		ParameterizedRowMapper<UserEntity> mapper = new ParameterizedRowMapper<UserEntity>() { 
//		    
//	        // notice the return type with respect to Java 5 covariant return types 
//	        public UserEntity mapRow(ResultSet rs, int rowNum) throws SQLException { 
//	        	UserEntity actor = new UserEntity(); 
//	        	actor.setRemark(rs.getString("remark"));
//	        	actor.setTxt1(rs.getString("txt1"));
//	        	actor.setTxt2(rs.getString("txt2"));
//	        	actor.setAccount(rs.getString("account"));
//	        	actor.setCtime(rs.getString("ctime"));
//	        	actor.setId(rs.getInt("id"));
//	        	actor.setName(rs.getString("name"));
//	        	actor.setPassword(rs.getString("password"));
//	        	actor.setRole(rs.getInt("role"));
//	            return actor; 
//	        }
//		};

		//ParameterizedRowMapper<UserEntity> mapper = new ParameterizedRowMapper<UserEntity>() { 
	    
        // notice the return type with respect to Java 5 covariant return types 
//        public UserEntity mapRow(ResultSet rs, int rowNum) throws SQLException { 
//        	UserEntity actor = new UserEntity(); 
//        	actor.setRemark(rs.getString("remark"));
//        	actor.setTxt1(rs.getString("txt1"));
//        	actor.setTxt2(rs.getString("txt2"));
//        	actor.setAccount(rs.getString("account"));
//        	actor.setCtime(rs.getString("ctime"));
//        	actor.setId(rs.getInt("id"));
//        	actor.setName(rs.getString("name"));
//        	actor.setPassword(rs.getString("password"));
//        	actor.setRole(rs.getInt("role"));
//            return actor; 
//        }
//	};
		return this.findByPage(true, page, "user_findAll", null, param,where,UserEntity.class);
	}

	public int findUserCount() {
		return this.findUserCount("user_findCount");
	}

//	public int findUserCount() {
//		String sql = "";
//			try {
//				sql = PropertiesUtil.load("user_findCount");
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		Map<String, ?> map = baseDao.find(sql, null);
//		long count = (Long)map.get("totalSize");
//		return (int) count;
//	}

}
